
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" />
  
  <!-- Licensed under the Apache 2.0 License -->
  <link rel="stylesheet" type="text/css" href="../../../../_static/fonts/open-sans/stylesheet.css" />
  <!-- Licensed under the SIL Open Font License -->
  <link rel="stylesheet" type="text/css" href="../../../../_static/fonts/source-serif-pro/source-serif-pro.css" />
  <link rel="stylesheet" type="text/css" href="../../../../_static/css/bootstrap.min.css" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
    <title>jmetal.lab.statistical_test.functions &#8212; jMetalPy 1.5.3 documentation</title>
    <link rel="stylesheet" href="../../../../_static/guzzle.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../../../_static/custom.css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    <script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
    <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true, "ignoreClass": "document", "processClass": "math|output_area"}})</script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
  
   

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">jMetalPy 1.5.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="container-wrapper">

      <div id="mobile-toggle">
        <a href="#"><span class="glyphicon glyphicon-align-justify" aria-hidden="true"></span></a>
      </div>
  <div id="left-column">
    <div class="sphinxsidebar"><a href="
    ../../../../index.html" class="text-logo">
    <img src="_static/jmetalpy.png" class="img-fluid" alt="jMetalPy 1.5.3 documentation">
    <br>
</a>

<div class="sidebar-block">
  <div class="sidebar-wrapper">
    Python version of the jMetal framework
  </div>
</div>
<div class="sidebar-block">
  <div class="sidebar-wrapper">
    <h2>Table Of Contents</h2>
  </div>
  <div class="sidebar-toc">
    
    
      <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../tutorials.html">Getting started</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../multiobjective.algorithms.html">Multi-objective algorithms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../singleobjective.algorithms.html">Single-objective algorithms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../operators.html">Operators</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../problems.html">Problems</a></li>
</ul>

    
  </div>
</div>
<div class="sidebar-block">
  <div class="sidebar-wrapper">
    <div id="main-search">
      <form class="form-inline" action="../../../../search.html" method="GET" role="form">
        <div class="input-group">
          <input name="q" type="text" class="form-control" placeholder="Search...">
        </div>
        <input type="hidden" name="check_keywords" value="yes" />
        <input type="hidden" name="area" value="default" />
      </form>
    </div>
  </div>
</div>
      
    </div>
  </div>
        <div id="right-column">
          
          <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
              <li class="breadcrumb-item"><a href="../../../../index.html">Docs</a></li>
              
              <li class="breadcrumb-item"><a href="../../../index.html">Module code</a></li>
              
              <li class="breadcrumb-item">jmetal.lab.statistical_test.functions</li>
            </ol>
          </nav>
          
          <div class="document clearer body">
            
  <h1>Source code for jmetal.lab.statistical_test.functions</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">scipy.stats</span> <span class="kn">import</span> <span class="n">chi2</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">binom</span><span class="p">,</span> <span class="n">norm</span>

<span class="kn">from</span> <span class="nn">jmetal.lab.statistical_test.apv_procedures</span> <span class="kn">import</span> <span class="o">*</span>


<div class="viewcode-block" id="ranks"><a class="viewcode-back" href="../../../../api/jmetal.lab.statistical_test.html#jmetal.lab.statistical_test.functions.ranks">[docs]</a><span class="k">def</span> <span class="nf">ranks</span><span class="p">(</span><span class="n">data</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">,</span> <span class="n">descending</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Computes the rank of the elements in data.</span>

<span class="sd">    :param data: 2-D matrix</span>
<span class="sd">    :param descending: boolean (default False). If true, rank is sorted in descending order.</span>
<span class="sd">    :return: ranks, where ranks[i][j] == rank of the i-th row w.r.t the j-th column.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">s</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">if</span> <span class="p">(</span><span class="n">descending</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">)</span> <span class="k">else</span> <span class="mi">1</span>

    <span class="c1"># Compute ranks. (ranks[i][j] == rank of the i-th treatment on the j-th sample.)</span>
    <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">ranks</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
            <span class="n">values</span><span class="p">,</span> <span class="n">indices</span><span class="p">,</span> <span class="n">rep</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">unique</span><span class="p">(</span>
                <span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">**</span> <span class="n">s</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sort</span><span class="p">((</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">**</span> <span class="n">s</span> <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:]),</span> <span class="n">return_index</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">return_counts</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="p">)</span>
            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
                <span class="n">ranks</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">+=</span> <span class="n">indices</span><span class="p">[</span><span class="n">values</span> <span class="o">==</span> <span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]]</span> <span class="o">+</span> \
                               <span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="n">rep</span><span class="p">[</span><span class="n">values</span> <span class="o">==</span> <span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ranks</span>
    <span class="k">elif</span> <span class="n">data</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">ranks</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="n">data</span><span class="o">.</span><span class="n">size</span><span class="p">,))</span>
        <span class="n">values</span><span class="p">,</span> <span class="n">indices</span><span class="p">,</span> <span class="n">rep</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">unique</span><span class="p">(</span>
            <span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">**</span> <span class="n">s</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sort</span><span class="p">((</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">**</span> <span class="n">s</span> <span class="o">*</span> <span class="n">data</span><span class="p">),</span> <span class="n">return_index</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">return_counts</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="p">)</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">size</span><span class="p">):</span>
            <span class="n">ranks</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+=</span> <span class="n">indices</span><span class="p">[</span><span class="n">values</span> <span class="o">==</span> <span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">+</span> \
                        <span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="n">rep</span><span class="p">[</span><span class="n">values</span> <span class="o">==</span> <span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ranks</span></div>


<div class="viewcode-block" id="sign_test"><a class="viewcode-back" href="../../../../api/jmetal.lab.statistical_test.html#jmetal.lab.statistical_test.functions.sign_test">[docs]</a><span class="k">def</span> <span class="nf">sign_test</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Given the results drawn from two algorithms/methods X and Y, the sign test analyses if</span>
<span class="sd">    there is a difference between X and Y.</span>

<span class="sd">    .. note:: Null Hypothesis: Pr(X&lt;Y)= 0.5</span>

<span class="sd">    :param data: An (n x 2) array or DataFrame contaning the results. In data, each column represents an algorithm and, and each row a problem.</span>
<span class="sd">    :return p_value: The associated p-value from the binomial distribution.</span>
<span class="sd">    :return bstat: Number of successes.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">==</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">values</span>

    <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">X</span><span class="p">,</span> <span class="n">Y</span> <span class="o">=</span> <span class="n">data</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">data</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">]</span>
        <span class="n">n_perf</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s1">&#39;Initialization ERROR. Incorrect number of dimensions for axis 1&#39;</span><span class="p">)</span>

    <span class="c1"># Compute the differences</span>
    <span class="n">Z</span> <span class="o">=</span> <span class="n">X</span> <span class="o">-</span> <span class="n">Y</span>
    <span class="c1"># Compute the number of pairs Z&lt;0</span>
    <span class="n">Wminus</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">Z</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span>
    <span class="c1"># If H_0 is true ---&gt; W follows Binomial(n,0.5)</span>
    <span class="n">p_value_minus</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">binom</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="n">k</span><span class="o">=</span><span class="n">Wminus</span><span class="p">,</span> <span class="n">p</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">n</span><span class="o">=</span><span class="n">n_perf</span><span class="p">)</span>

    <span class="c1"># Compute the number of pairs Z&gt;0</span>
    <span class="n">Wplus</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">Z</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span>
    <span class="c1"># If H_0 is true ---&gt; W follows Binomial(n,0.5)</span>
    <span class="n">p_value_plus</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">binom</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="n">k</span><span class="o">=</span><span class="n">Wplus</span><span class="p">,</span> <span class="n">p</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">n</span><span class="o">=</span><span class="n">n_perf</span><span class="p">)</span>

    <span class="n">p_value</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="nb">min</span><span class="p">([</span><span class="n">p_value_minus</span><span class="p">,</span> <span class="n">p_value_plus</span><span class="p">])</span>

    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">Wminus</span><span class="p">,</span> <span class="n">Wplus</span><span class="p">,</span> <span class="n">p_value</span><span class="p">]),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;Num X&lt;Y&#39;</span><span class="p">,</span> <span class="s1">&#39;Num X&gt;Y&#39;</span><span class="p">,</span> <span class="s1">&#39;p-value&#39;</span><span class="p">],</span>
                        <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;Results&#39;</span><span class="p">])</span></div>


<div class="viewcode-block" id="friedman_test"><a class="viewcode-back" href="../../../../api/jmetal.lab.statistical_test.html#jmetal.lab.statistical_test.functions.friedman_test">[docs]</a><span class="k">def</span> <span class="nf">friedman_test</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Friedman ranking test.</span>

<span class="sd">    ..note:: Null Hypothesis: In a set of k (&gt;=2) treaments (or tested algorithms), all the treatments are equivalent, so their average ranks should be equal.</span>

<span class="sd">    :param data: An (n x 2) array or DataFrame contaning the results. In data, each column represents an algorithm and, and each row a problem.</span>
<span class="sd">    :return p_value: The associated p-value.</span>
<span class="sd">    :return friedman_stat: Friedman&#39;s chi-square.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Initial Checking</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">==</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">values</span>

    <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">n_samples</span><span class="p">,</span> <span class="n">k</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s1">&#39;Initialization ERROR. Incorrect number of array dimensions&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s1">&#39;Initialization Error. Incorrect number of dimensions for axis 1.&#39;</span><span class="p">)</span>

    <span class="c1"># Compute ranks.</span>
    <span class="n">datarank</span> <span class="o">=</span> <span class="n">ranks</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

    <span class="c1"># Compute for each algorithm the ranking average.</span>
    <span class="n">avranks</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">datarank</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

    <span class="c1"># Get Friedman statistics</span>
    <span class="n">friedman_stat</span> <span class="o">=</span> <span class="p">(</span><span class="mf">12.0</span> <span class="o">*</span> <span class="n">n_samples</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">k</span> <span class="o">*</span> <span class="p">(</span><span class="n">k</span> <span class="o">+</span> <span class="mf">1.0</span><span class="p">))</span> <span class="o">*</span> \
                    <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">avranks</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">k</span> <span class="o">*</span> <span class="p">(</span><span class="n">k</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">/</span> <span class="mf">4.0</span><span class="p">)</span>

    <span class="c1"># Compute p-value</span>
    <span class="n">p_value</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">-</span> <span class="n">chi2</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="n">friedman_stat</span><span class="p">,</span> <span class="n">df</span><span class="o">=</span><span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)))</span>

    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">friedman_stat</span><span class="p">,</span> <span class="n">p_value</span><span class="p">]),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;Friedman-statistic&#39;</span><span class="p">,</span> <span class="s1">&#39;p-value&#39;</span><span class="p">],</span>
                        <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;Results&#39;</span><span class="p">])</span></div>


<div class="viewcode-block" id="friedman_aligned_rank_test"><a class="viewcode-back" href="../../../../api/jmetal.lab.statistical_test.html#jmetal.lab.statistical_test.functions.friedman_aligned_rank_test">[docs]</a><span class="k">def</span> <span class="nf">friedman_aligned_rank_test</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Method of aligned ranks for the Friedman test.</span>

<span class="sd">    ..note:: Null Hypothesis: In a set of k (&gt;=2) treaments (or tested algorithms), all the treatments are equivalent, so their average ranks should be equal.</span>

<span class="sd">    :param data: An (n x 2) array or DataFrame contaning the results. In data, each column represents an algorithm and, and each row a problem.</span>
<span class="sd">    :return p_value: The associated p-value.</span>
<span class="sd">    :return aligned_rank_stat: Friedman&#39;s aligned rank chi-square statistic.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Initial Checking</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">==</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">values</span>

    <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">n_samples</span><span class="p">,</span> <span class="n">k</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s1">&#39;Initialization ERROR. Incorrect number of array dimensions&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s1">&#39;Initialization Error. Incorrect number of dimensions for axis 1.&#39;</span><span class="p">)</span>

    <span class="c1"># Compute the average value achieved by all algorithms in each problem</span>
    <span class="n">control</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
    <span class="c1"># Compute the difference between control an data</span>
    <span class="n">diff</span> <span class="o">=</span> <span class="p">[</span><span class="n">data</span><span class="p">[:,</span> <span class="n">j</span><span class="p">]</span> <span class="o">-</span> <span class="n">control</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span>
    <span class="c1"># rank diff</span>
    <span class="n">alignedRanks</span> <span class="o">=</span> <span class="n">ranks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">diff</span><span class="p">))</span>
    <span class="n">alignedRanks</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">alignedRanks</span><span class="p">,</span> <span class="n">newshape</span><span class="o">=</span><span class="p">(</span><span class="n">n_samples</span><span class="p">,</span> <span class="n">k</span><span class="p">),</span> <span class="n">order</span><span class="o">=</span><span class="s1">&#39;F&#39;</span><span class="p">)</span>

    <span class="c1"># Compute statistic</span>
    <span class="n">Rhat_i</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">alignedRanks</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">Rhat_j</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">alignedRanks</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
    <span class="n">si</span><span class="p">,</span> <span class="n">sj</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">Rhat_i</span> <span class="o">**</span> <span class="mi">2</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">Rhat_j</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span>

    <span class="n">A</span> <span class="o">=</span> <span class="n">sj</span> <span class="o">-</span> <span class="p">(</span><span class="n">k</span> <span class="o">*</span> <span class="n">n_samples</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">/</span> <span class="mf">4.0</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">k</span> <span class="o">*</span> <span class="n">n_samples</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
    <span class="n">B1</span> <span class="o">=</span> <span class="p">(</span><span class="n">k</span> <span class="o">*</span> <span class="n">n_samples</span> <span class="o">*</span> <span class="p">(</span><span class="n">k</span> <span class="o">*</span> <span class="n">n_samples</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">k</span> <span class="o">*</span> <span class="n">n_samples</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mf">6.0</span><span class="p">)</span>
    <span class="n">B2</span> <span class="o">=</span> <span class="n">si</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">k</span><span class="p">)</span>

    <span class="n">alignedRanks_stat</span> <span class="o">=</span> <span class="p">((</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">A</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">B1</span> <span class="o">-</span> <span class="n">B2</span><span class="p">)</span>

    <span class="n">p_value</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">chi2</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="n">alignedRanks_stat</span><span class="p">,</span> <span class="n">df</span><span class="o">=</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">alignedRanks_stat</span><span class="p">,</span> <span class="n">p_value</span><span class="p">]),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;Aligned Rank stat&#39;</span><span class="p">,</span> <span class="s1">&#39;p-value&#39;</span><span class="p">],</span>
                        <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;Results&#39;</span><span class="p">])</span></div>


<div class="viewcode-block" id="quade_test"><a class="viewcode-back" href="../../../../api/jmetal.lab.statistical_test.html#jmetal.lab.statistical_test.functions.quade_test">[docs]</a><span class="k">def</span> <span class="nf">quade_test</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Quade test.</span>

<span class="sd">    ..note:: Null Hypothesis: In a set of k (&gt;=2) treaments (or tested algorithms), all the treatments are equivalent, so their average ranks should be equal.</span>

<span class="sd">    :param data: An (n x 2) array or DataFrame contaning the results. In data, each column represents an algorithm and, and each row a problem.</span>
<span class="sd">    :return p_value: The associated p-value from the F-distribution.</span>
<span class="sd">    :return fq: Computed F-value.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Initial Checking</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">==</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">values</span>

    <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">n_samples</span><span class="p">,</span> <span class="n">k</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s1">&#39;Initialization ERROR. Incorrect number of array dimensions&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s1">&#39;Initialization Error. Incorrect number of dimensions for axis 1.&#39;</span><span class="p">)</span>

    <span class="c1"># Compute ranks.</span>
    <span class="n">datarank</span> <span class="o">=</span> <span class="n">ranks</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
    <span class="c1"># Compute the range of each problem</span>
    <span class="n">problemRange</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
    <span class="c1"># Compute problem rank</span>
    <span class="n">problemRank</span> <span class="o">=</span> <span class="n">ranks</span><span class="p">(</span><span class="n">problemRange</span><span class="p">)</span>

    <span class="c1"># Compute S_stat: weight of each observation within the problem, adjusted to reflect</span>
    <span class="c1"># the significance of the problem when it appears.</span>
    <span class="n">S_stat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">n_samples</span><span class="p">,</span> <span class="n">k</span><span class="p">))</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_samples</span><span class="p">):</span>
        <span class="n">S_stat</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">problemRank</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">datarank</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:]</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="n">k</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>
    <span class="n">Salg</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">S_stat</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

    <span class="c1"># Compute Fq (Quade Test statistic) and associated p_value</span>
    <span class="n">A</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">S_stat</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span>
    <span class="n">B</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">Salg</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">n_samples</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">A</span> <span class="o">==</span> <span class="n">B</span><span class="p">:</span>
        <span class="n">Fq</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">Inf</span>
        <span class="n">p_value</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">math</span><span class="o">.</span><span class="n">factorial</span><span class="p">(</span><span class="n">k</span><span class="p">)))</span> <span class="o">**</span> <span class="p">(</span><span class="n">n_samples</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">Fq</span> <span class="o">=</span> <span class="p">(</span><span class="n">n_samples</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">*</span> <span class="n">B</span> <span class="o">/</span> <span class="p">(</span><span class="n">A</span> <span class="o">-</span> <span class="n">B</span><span class="p">)</span>
        <span class="n">p_value</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">f</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="n">Fq</span><span class="p">,</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">n_samples</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span>

    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">Fq</span><span class="p">,</span> <span class="n">p_value</span><span class="p">]),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;Quade Test statistic&#39;</span><span class="p">,</span> <span class="s1">&#39;p-value&#39;</span><span class="p">],</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;Results&#39;</span><span class="p">])</span></div>


<div class="viewcode-block" id="friedman_ph_test"><a class="viewcode-back" href="../../../../api/jmetal.lab.statistical_test.html#jmetal.lab.statistical_test.functions.friedman_ph_test">[docs]</a><span class="k">def</span> <span class="nf">friedman_ph_test</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">apv_procedure</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Friedman post-hoc test.</span>

<span class="sd">    :param data: An (n x 2) array or DataFrame contaning the results. In data, each column represents an algorithm and, and each row a problem.</span>
<span class="sd">    :param control: optional int or string. Default None. Index or Name of the control algorithm. If control = None all FriedmanPosHocTest considers all possible comparisons among algorithms.</span>
<span class="sd">    :param apv_procedure: optional string. Default None.</span>
<span class="sd">        Name of the procedure for computing adjusted p-values. If apv_procedure</span>
<span class="sd">        is None, adjusted p-value are not computed, else the values are computed</span>
<span class="sd">        according to the specified procedure:</span>
<span class="sd">        For 1 vs all comparisons.</span>
<span class="sd">            {&#39;Bonferroni&#39;, &#39;Holm&#39;, &#39;Hochberg&#39;, &#39;Holland&#39;, &#39;Finner&#39;, &#39;Li&#39;}</span>
<span class="sd">        For all vs all coparisons.</span>
<span class="sd">            {&#39;Shaffer&#39;, &#39;Holm&#39;, &#39;Nemenyi&#39;}</span>

<span class="sd">    :return z_values: Test statistic.</span>
<span class="sd">    :return p_values: The p-value according to the Studentized range distribution.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Initial Checking</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">==</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
        <span class="n">algorithms</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">columns</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">values</span>
    <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="n">algorithms</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">&#39;Alg</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">alg</span> <span class="k">for</span> <span class="n">alg</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])])</span>

    <span class="k">if</span> <span class="n">control</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">index</span> <span class="o">=</span> <span class="n">algorithms</span>
    <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">control</span><span class="p">)</span> <span class="o">==</span> <span class="nb">int</span><span class="p">:</span>
        <span class="n">index</span> <span class="o">=</span> <span class="p">[</span><span class="n">algorithms</span><span class="p">[</span><span class="n">control</span><span class="p">]]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">index</span> <span class="o">=</span> <span class="p">[</span><span class="n">control</span><span class="p">]</span>

    <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">n_samples</span><span class="p">,</span> <span class="n">k</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s1">&#39;Initialization ERROR. Incorrect number of array dimensions.&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s1">&#39;Initialization Error. Incorrect number of dimensions for axis 1.&#39;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">control</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">control</span><span class="p">)</span> <span class="o">==</span> <span class="nb">int</span> <span class="ow">and</span> <span class="n">control</span> <span class="o">&gt;=</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Initialization ERROR. control is out of bounds&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">control</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span> <span class="ow">and</span> <span class="n">control</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">algorithms</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s1">&#39;Initialization ERROR. </span><span class="si">%s</span><span class="s1"> is not a column name of data&#39;</span> <span class="o">%</span> <span class="n">control</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">apv_procedure</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">apv_procedure</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;Bonferroni&#39;</span><span class="p">,</span> <span class="s1">&#39;Holm&#39;</span><span class="p">,</span> <span class="s1">&#39;Hochberg&#39;</span><span class="p">,</span> <span class="s1">&#39;Hommel&#39;</span><span class="p">,</span> <span class="s1">&#39;Holland&#39;</span><span class="p">,</span> <span class="s1">&#39;Finner&#39;</span><span class="p">,</span> <span class="s1">&#39;Li&#39;</span><span class="p">,</span> <span class="s1">&#39;Shaffer&#39;</span><span class="p">,</span>
                                 <span class="s1">&#39;Nemenyi&#39;</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s1">&#39;Initialization ERROR. Incorrect value for APVprocedure.&#39;</span><span class="p">)</span>

    <span class="c1"># Compute ranks.</span>
    <span class="n">datarank</span> <span class="o">=</span> <span class="n">ranks</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
    <span class="c1"># Compute for each algorithm the ranking average.</span>
    <span class="n">avranks</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">datarank</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

    <span class="c1"># Compute z-values</span>
    <span class="n">aux</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">((</span><span class="n">k</span> <span class="o">*</span> <span class="p">(</span><span class="n">k</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span> <span class="o">/</span> <span class="p">(</span><span class="mf">6.0</span> <span class="o">*</span> <span class="n">n_samples</span><span class="p">))</span>

    <span class="k">if</span> <span class="n">control</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">z</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">k</span><span class="p">,</span> <span class="n">k</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">k</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
                <span class="n">z</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">avranks</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">avranks</span><span class="p">[</span><span class="n">j</span><span class="p">])</span> <span class="o">/</span> <span class="n">aux</span>
        <span class="n">z</span> <span class="o">+=</span> <span class="n">z</span><span class="o">.</span><span class="n">T</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">control</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span><span class="p">:</span>
            <span class="n">control</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">algorithms</span> <span class="o">==</span> <span class="n">control</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">z</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="n">k</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">k</span><span class="p">):</span>
            <span class="n">z</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">avranks</span><span class="p">[</span><span class="n">control</span><span class="p">]</span> <span class="o">-</span> <span class="n">avranks</span><span class="p">[</span><span class="n">j</span><span class="p">])</span> <span class="o">/</span> <span class="n">aux</span>

    <span class="c1"># Compute associated p-value</span>
    <span class="n">p_value</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">-</span> <span class="n">norm</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>

    <span class="n">pvalues_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">p_value</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="n">index</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">algorithms</span><span class="p">)</span>
    <span class="n">zvalues_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">z</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="n">index</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">algorithms</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">apv_procedure</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">zvalues_df</span><span class="p">,</span> <span class="n">pvalues_df</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Bonferroni&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">bonferroni_dunn</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Holm&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">holm</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Hochberg&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">hochberg</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Holland&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">holland</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Finner&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">finner</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Li&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">li</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Shaffer&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">shaffer</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Nemenyi&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">nemenyi</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">zvalues_df</span><span class="p">,</span> <span class="n">pvalues_df</span><span class="p">,</span> <span class="n">ap_vs_df</span></div>


<div class="viewcode-block" id="friedman_aligned_ph_test"><a class="viewcode-back" href="../../../../api/jmetal.lab.statistical_test.html#jmetal.lab.statistical_test.functions.friedman_aligned_ph_test">[docs]</a><span class="k">def</span> <span class="nf">friedman_aligned_ph_test</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">apv_procedure</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Friedman Aligned Ranks post-hoc test.</span>

<span class="sd">    :param data: An (n x 2) array or DataFrame contaning the results. In data, each column represents an algorithm and, and each row a problem.</span>
<span class="sd">    :param control: optional int or string. Default None. Index or Name of the control algorithm. If control = None all FriedmanPosHocTest considers all possible comparisons among algorithms.</span>
<span class="sd">    :param apv_procedure: optional string. Default None.</span>
<span class="sd">        Name of the procedure for computing adjusted p-values. If apv_procedure</span>
<span class="sd">        is None, adjusted p-value are not computed, else the values are computed</span>
<span class="sd">        according to the specified procedure:</span>
<span class="sd">        For 1 vs all comparisons.</span>
<span class="sd">            {&#39;Bonferroni&#39;, &#39;Holm&#39;, &#39;Hochberg&#39;, &#39;Holland&#39;, &#39;Finner&#39;, &#39;Li&#39;}</span>
<span class="sd">        For all vs all coparisons.</span>
<span class="sd">            {&#39;Shaffer&#39;, &#39;Holm&#39;, &#39;Nemenyi&#39;}</span>

<span class="sd">    :return z_values: Test statistic.</span>
<span class="sd">    :return p_values: The p-value according to the Studentized range distribution.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Initial Checking</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">==</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
        <span class="n">algorithms</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">columns</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">values</span>
    <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="n">algorithms</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">&#39;Alg</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">alg</span> <span class="k">for</span> <span class="n">alg</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])])</span>

    <span class="k">if</span> <span class="n">control</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">index</span> <span class="o">=</span> <span class="n">algorithms</span>
    <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">control</span><span class="p">)</span> <span class="o">==</span> <span class="nb">int</span><span class="p">:</span>
        <span class="n">index</span> <span class="o">=</span> <span class="p">[</span><span class="n">algorithms</span><span class="p">[</span><span class="n">control</span><span class="p">]]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">index</span> <span class="o">=</span> <span class="p">[</span><span class="n">control</span><span class="p">]</span>

    <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">n_samples</span><span class="p">,</span> <span class="n">k</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s1">&#39;Initialization ERROR. Incorrect number of array dimensions.&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s1">&#39;Initialization Error. Incorrect number of dimensions for axis 1.&#39;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">control</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">control</span><span class="p">)</span> <span class="o">==</span> <span class="nb">int</span> <span class="ow">and</span> <span class="n">control</span> <span class="o">&gt;=</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Initialization ERROR. control is out of bounds&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">control</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span> <span class="ow">and</span> <span class="n">control</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">algorithms</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s1">&#39;Initialization ERROR. </span><span class="si">%s</span><span class="s1"> is not a column name of data&#39;</span> <span class="o">%</span> <span class="n">control</span><span class="p">)</span>

    <span class="c1"># Compute the average value achieved by all algorithms in each problem</span>
    <span class="n">problemmean</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
    <span class="c1"># Compute the difference between control an data</span>
    <span class="n">diff</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">n_samples</span><span class="p">,</span> <span class="n">k</span><span class="p">))</span>
    <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">k</span><span class="p">):</span>
        <span class="n">diff</span><span class="p">[:,</span> <span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[:,</span> <span class="n">j</span><span class="p">]</span> <span class="o">-</span> <span class="n">problemmean</span>

    <span class="n">alignedRanks</span> <span class="o">=</span> <span class="n">ranks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">diff</span><span class="p">))</span>
    <span class="n">alignedRanks</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">alignedRanks</span><span class="p">,</span> <span class="n">newshape</span><span class="o">=</span><span class="p">(</span><span class="n">n_samples</span><span class="p">,</span> <span class="n">k</span><span class="p">))</span>

    <span class="c1"># Average ranks</span>
    <span class="n">avranks</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">alignedRanks</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

    <span class="c1"># Compute test statistics</span>
    <span class="n">aux</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">k</span> <span class="o">*</span> <span class="p">(</span><span class="n">n_samples</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mf">6.0</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">control</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">z</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">k</span><span class="p">,</span> <span class="n">k</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">k</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
                <span class="n">z</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">avranks</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">avranks</span><span class="p">[</span><span class="n">j</span><span class="p">])</span> <span class="o">*</span> <span class="n">aux</span>
        <span class="n">z</span> <span class="o">+=</span> <span class="n">z</span><span class="o">.</span><span class="n">T</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">control</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span><span class="p">:</span>
            <span class="n">control</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">algorithms</span> <span class="o">==</span> <span class="n">control</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">z</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="n">k</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">k</span><span class="p">):</span>
            <span class="n">z</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">avranks</span><span class="p">[</span><span class="n">control</span><span class="p">]</span> <span class="o">-</span> <span class="n">avranks</span><span class="p">[</span><span class="n">j</span><span class="p">])</span> <span class="o">*</span> <span class="n">aux</span>

    <span class="c1"># Compute associated p-value</span>
    <span class="n">p_value</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">-</span> <span class="n">norm</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>

    <span class="n">pvalues_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">p_value</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="n">index</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">algorithms</span><span class="p">)</span>
    <span class="n">zvalues_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">z</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="n">index</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">algorithms</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">apv_procedure</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">zvalues_df</span><span class="p">,</span> <span class="n">pvalues_df</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Bonferroni&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">bonferroni_dunn</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Holm&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">holm</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Hochberg&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">hochberg</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Holland&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">holland</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Finner&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">finner</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Li&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">li</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Shaffer&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">shaffer</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Nemenyi&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">nemenyi</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">zvalues_df</span><span class="p">,</span> <span class="n">pvalues_df</span><span class="p">,</span> <span class="n">ap_vs_df</span></div>


<div class="viewcode-block" id="quade_ph_test"><a class="viewcode-back" href="../../../../api/jmetal.lab.statistical_test.html#jmetal.lab.statistical_test.functions.quade_ph_test">[docs]</a><span class="k">def</span> <span class="nf">quade_ph_test</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">apv_procedure</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Quade post-hoc test.</span>

<span class="sd">    :param data: An (n x 2) array or DataFrame contaning the results. In data, each column represents an algorithm and, and each row a problem.</span>
<span class="sd">    :param control: optional int or string. Default None. Index or Name of the control algorithm. If control = None all FriedmanPosHocTest considers all possible comparisons among algorithms.</span>
<span class="sd">    :param apv_procedure: optional string. Default None.</span>
<span class="sd">        Name of the procedure for computing adjusted p-values. If apv_procedure</span>
<span class="sd">        is None, adjusted p-value are not computed, else the values are computed</span>
<span class="sd">        according to the specified procedure:</span>
<span class="sd">        For 1 vs all comparisons.</span>
<span class="sd">            {&#39;Bonferroni&#39;, &#39;Holm&#39;, &#39;Hochberg&#39;, &#39;Holland&#39;, &#39;Finner&#39;, &#39;Li&#39;}</span>
<span class="sd">        For all vs all coparisons.</span>
<span class="sd">            {&#39;Shaffer&#39;, &#39;Holm&#39;, &#39;Nemenyi&#39;}</span>

<span class="sd">    :return z_values: Test statistic.</span>
<span class="sd">    :return p_values: The p-value according to the Studentized range distribution.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Initial Checking</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">==</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
        <span class="n">algorithms</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">columns</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">values</span>
    <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="n">algorithms</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">&#39;Alg</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">alg</span> <span class="k">for</span> <span class="n">alg</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])])</span>

    <span class="k">if</span> <span class="n">control</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">index</span> <span class="o">=</span> <span class="n">algorithms</span>
    <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">control</span><span class="p">)</span> <span class="o">==</span> <span class="nb">int</span><span class="p">:</span>
        <span class="n">index</span> <span class="o">=</span> <span class="p">[</span><span class="n">algorithms</span><span class="p">[</span><span class="n">control</span><span class="p">]]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">index</span> <span class="o">=</span> <span class="p">[</span><span class="n">control</span><span class="p">]</span>

    <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">n_samples</span><span class="p">,</span> <span class="n">k</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s1">&#39;Initialization ERROR. Incorrect number of array dimensions.&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s1">&#39;Initialization Error. Incorrect number of dimensions for axis 1.&#39;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">control</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">control</span><span class="p">)</span> <span class="o">==</span> <span class="nb">int</span> <span class="ow">and</span> <span class="n">control</span> <span class="o">&gt;=</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Initialization ERROR. control is out of bounds&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">control</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span> <span class="ow">and</span> <span class="n">control</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">algorithms</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s1">&#39;Initialization ERROR. </span><span class="si">%s</span><span class="s1"> is not a column name of data&#39;</span> <span class="o">%</span> <span class="n">control</span><span class="p">)</span>

    <span class="c1"># Compute ranks.</span>
    <span class="n">datarank</span> <span class="o">=</span> <span class="n">ranks</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
    <span class="c1"># Compute the range of each problem</span>
    <span class="n">problemRange</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
    <span class="c1"># Compute problem rank</span>
    <span class="n">problemRank</span> <span class="o">=</span> <span class="n">ranks</span><span class="p">(</span><span class="n">problemRange</span><span class="p">)</span>

    <span class="c1"># Compute average rakings</span>
    <span class="n">W</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">n_samples</span><span class="p">,</span> <span class="n">k</span><span class="p">))</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_samples</span><span class="p">):</span>
        <span class="n">W</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">problemRank</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">datarank</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:]</span>
    <span class="n">avranks</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">W</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">n_samples</span> <span class="o">*</span> <span class="p">(</span><span class="n">n_samples</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>
    <span class="c1"># Compute test statistics</span>
    <span class="n">aux</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">k</span> <span class="o">*</span> <span class="p">(</span><span class="n">k</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">n_samples</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span>
                        <span class="p">(</span><span class="mf">18.0</span> <span class="o">*</span> <span class="n">n_samples</span> <span class="o">*</span> <span class="p">(</span><span class="n">n_samples</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)))</span>
    <span class="k">if</span> <span class="n">control</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">z</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">k</span><span class="p">,</span> <span class="n">k</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">k</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
                <span class="n">z</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">avranks</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">avranks</span><span class="p">[</span><span class="n">j</span><span class="p">])</span> <span class="o">*</span> <span class="n">aux</span>
        <span class="n">z</span> <span class="o">+=</span> <span class="n">z</span><span class="o">.</span><span class="n">T</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">control</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span><span class="p">:</span>
            <span class="n">control</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">algorithms</span> <span class="o">==</span> <span class="n">control</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">z</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="n">k</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">k</span><span class="p">):</span>
            <span class="n">z</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">avranks</span><span class="p">[</span><span class="n">control</span><span class="p">]</span> <span class="o">-</span> <span class="n">avranks</span><span class="p">[</span><span class="n">j</span><span class="p">])</span> <span class="o">*</span> <span class="n">aux</span>

    <span class="c1"># Compute associated p-value</span>
    <span class="n">p_value</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">-</span> <span class="n">norm</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>

    <span class="n">pvalues_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">p_value</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="n">index</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">algorithms</span><span class="p">)</span>
    <span class="n">zvalues_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">z</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="n">index</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">algorithms</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">apv_procedure</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">zvalues_df</span><span class="p">,</span> <span class="n">pvalues_df</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Bonferroni&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">bonferroni_dunn</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Holm&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">holm</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Hochberg&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">hochberg</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Holland&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">holland</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Finner&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">finner</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Li&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">li</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">,</span> <span class="n">control</span><span class="o">=</span><span class="n">control</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Shaffer&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">shaffer</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">apv_procedure</span> <span class="o">==</span> <span class="s1">&#39;Nemenyi&#39;</span><span class="p">:</span>
            <span class="n">ap_vs_df</span> <span class="o">=</span> <span class="n">nemenyi</span><span class="p">(</span><span class="n">pvalues_df</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">zvalues_df</span><span class="p">,</span> <span class="n">pvalues_df</span><span class="p">,</span> <span class="n">ap_vs_df</span></div>
</pre></div>

          </div>
            
        </div>
        <div class="clearfix"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">jMetalPy 1.5.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
<script type="text/javascript">
  $("#mobile-toggle a").click(function () {
    $("#left-column").toggle();
  });
</script>
<script type="text/javascript" src="../../../../_static/js/bootstrap.js"></script>
  <div class="footer">
    &copy; Copyright 2019, Antonio Benítez-Hidalgo. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
  </div>
  </body>
</html>